package game;

import gameconsts.EditorConsts;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashSet;

import javax.swing.JPanel;

public class GameViewArea
	extends JPanel
	implements MouseListener, MouseMotionListener, Runnable
{
	private static final long serialVersionUID = -2707046411228228140L;
	
	private volatile boolean m_animate = true;
	
	private int m_animationSleep = 100; //milliseconds
	
	public IDrawableRectangle getPlatformAt(int x, int y)
	{
		for (IDrawableRectangle currentPlatformComponent : Game.gameComponents() )
		{
			if (currentPlatformComponent.bounds().contains(x, y) )
			{
				return currentPlatformComponent;
			}
		}
		
		return null;
	}
	
	private void paintPlatformComponents(Graphics g)
	{
		for (IDrawableRectangle currentPlatformComponent : Game.gameComponents() )
		{
			currentPlatformComponent.paintComponent(g);
		}
	}
	
	public void startAnimation()
	{
		new Thread(this).start();
	}
	
	public GameViewArea()
	{
		setPreferredSize(new Dimension(EditorConsts.gameViewAreaWidth, EditorConsts.gameViewAreaHeight) );

		Game.gameComponents(new HashSet<IDrawableRectangle>() );
		addMouseMotionListener(this);
		addMouseListener(this);
		setVisible(true);
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		g.setColor(Color.BLUE);
		g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
		paintPlatformComponents(g);
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		;
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		IDrawableRectangle selectedComp = getPlatformAt(e.getX(), e.getY() );
		Game.selectedComp(selectedComp);
		
		if (Game.noComponentSelected() )
		{
			Game.selectedComp(new DrawableRectangleImpl(e.getX(), e.getY() ) );
			Game.gameComponents().add(Game.selectedComp() );
		}
	}
	
	@Override
	public void mouseReleased(MouseEvent e)
	{
		//Game.selectedComp(null);
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e)
	{
		if (!Game.noComponentSelected() )
		{
			IDrawableRectangle platformAtEventXY = getPlatformAt(e.getX(), e.getY() );
			if  (platformAtEventXY == null || platformAtEventXY == Game.selectedComp() )
			{
				Game.selectedComp().width(e.getX() - Game.selectedComp().x() );
				Game.selectedComp().height(e.getY() - Game.selectedComp().y() );
			}
		}
	}

	@Override
	public void mouseMoved(MouseEvent e)
	{
		;
	}

	@Override
	public void run()
	{
		while (true)
		{
			if (m_animate)
			{
				repaint();
			}
			
			try
			{
				Thread.sleep(m_animationSleep);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
	
}
